package com.fb.fbvedioplayer

import android.content.Intent
import android.content.pm.ActivityInfo
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import cn.jzvd.*
import com.bumptech.glide.Glide
import com.fb.fbvedioplayer.CustomView.FBVideoPlayerStandard
import com.fb.fbvedioplayer.CustomView.MediaIjkplayer
import kotlinx.android.synthetic.main.activity_main.*

/**
 * <pre>
 *     author : fb
 *     e-mail : 469517834@qq.com
 *     time   : 2018/04/13
 *     desc   : 该activity是各种实现的入口
 * </pre>
 */
class ActivityMain : AppCompatActivity(), View.OnClickListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
//        initData()  //单视频播放
        initData2()   //多视频播放

        //设置播放器动作监听
        JZVideoPlayer.setJzUserAction(MyUserActionStandard())
        //设置用哪一个内核，默认系统自带
//        JZVideoPlayer.setMediaInterface(JZMediaSystem())
        JZVideoPlayer.setMediaInterface(MediaIjkplayer())
    }

    private fun initView() {
        //按钮监听
        video_size_btn.setOnClickListener(this)
        listview_btn.setOnClickListener(this)
        listview_tiny_btn.setOnClickListener(this)
        viewpager_btn.setOnClickListener(this)
        webview_btn.setOnClickListener(this)
    }

    private fun initData2() {
        //将各种清晰度的版本转换
        var url0 =ApplicationDemo.getProxy(this).getProxyUrl(VideoConstant.videoUrls[0])
        var url1 =ApplicationDemo.getProxy(this).getProxyUrl(VideoConstant.videoUrls[1])
        var url2 =ApplicationDemo.getProxy(this).getProxyUrl(VideoConstant.videoUrls[2])

        main_video_vp.setVideo(url0,url1,url2,"《创客说》|用跑酷改变你生活的方式", FBVideoPlayerStandard.SCREEN_WINDOW_NORMAL)
        //设置全屏时默认是横屏还是竖屏(不写默认是竖屏)
        FBVideoPlayerStandard.FULLSCREEN_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        //设置封面图
        Glide.with(this).load("http://caijingapp-test.oss-cn-shanghai.aliyuncs.com/Repository/img/69f7f88d-c75f-4424-8e80-2849e3ad00f4").into(main_video_vp.thumbImageView)
    }

    private fun initData() {
        //给播放器赋值
        var url =ApplicationDemo.getProxy(this).getProxyUrl(VideoConstant.videoUrlList[0])
        main_video_vp.setUp(url, FBVideoPlayerStandard.SCREEN_WINDOW_NORMAL, VideoConstant.videoTitles[0])
        //设置封面图
        Glide.with(this).load(VideoConstant.videoThumbList[0]).into(main_video_vp.thumbImageView)
    }

    override fun onClick(view: View?) {
        when(view!!.id){//得到控件id
            R.id.video_size_btn ->   startActivity(Intent(this, ActivityRotationVideoSize::class.java))
            R.id.listview_btn -> startActivity(Intent(this, ActivityRecyclerView::class.java))
            R.id.listview_tiny_btn -> startActivity(Intent(this, ActivityRecyclerViewTiny::class.java))
            R.id.viewpager_btn -> startActivity(Intent(this, ActivityViewpager::class.java))
            R.id.webview_btn -> startActivity(Intent(this, ActivityWebView::class.java))
        }
    }
   //很重要 必须写才能保证退出后视频流得到释放
    override fun onPause() {
        super.onPause()
       FBVideoPlayerStandard.releaseAllVideos()
    }
    //很重要 必须写才能保证退出后视频流得到释放
    override fun onBackPressed() {
        if (FBVideoPlayerStandard.backPress()) {
            return
        }
        super.onBackPressed()
    }
    /**
     * 实际项目中不用写
     * 这只是给埋点统计用户数据用的，不能写和播放相关的逻辑，监听事件请参考MyJZVideoPlayerStandard，复写函数取得相应事件
     */
    internal inner class MyUserActionStandard : JZUserActionStandard {

        override fun onEvent(type: Int, url: Any, screen: Int, vararg objects: Any) {
            when (type) {
                JZUserAction.ON_CLICK_START_ICON -> Log.i("USER_EVENT", "ON_CLICK_START_ICON" + " title is : " + (if (objects.size == 0) "" else objects[0]) + " url is : " + url + " screen is : " + screen)
                JZUserAction.ON_CLICK_START_ERROR -> Log.i("USER_EVENT", "ON_CLICK_START_ERROR" + " title is : " + (if (objects.size == 0) "" else objects[0]) + " url is : " + url + " screen is : " + screen)
                JZUserAction.ON_CLICK_START_AUTO_COMPLETE -> Log.i("USER_EVENT", "ON_CLICK_START_AUTO_COMPLETE" + " title is : " + (if (objects.size == 0) "" else objects[0]) + " url is : " + url + " screen is : " + screen)
                JZUserAction.ON_CLICK_PAUSE -> Log.i("USER_EVENT", "ON_CLICK_PAUSE" + " title is : " + (if (objects.size == 0) "" else objects[0]) + " url is : " + url + " screen is : " + screen)
                JZUserAction.ON_CLICK_RESUME -> Log.i("USER_EVENT", "ON_CLICK_RESUME" + " title is : " + (if (objects.size == 0) "" else objects[0]) + " url is : " + url + " screen is : " + screen)
                JZUserAction.ON_SEEK_POSITION -> Log.i("USER_EVENT", "ON_SEEK_POSITION" + " title is : " + (if (objects.size == 0) "" else objects[0]) + " url is : " + url + " screen is : " + screen)
                JZUserAction.ON_AUTO_COMPLETE -> Log.i("USER_EVENT", "ON_AUTO_COMPLETE" + " title is : " + (if (objects.size == 0) "" else objects[0]) + " url is : " + url + " screen is : " + screen)
                JZUserAction.ON_ENTER_FULLSCREEN -> Log.i("USER_EVENT", "ON_ENTER_FULLSCREEN" + " title is : " + (if (objects.size == 0) "" else objects[0]) + " url is : " + url + " screen is : " + screen)
                JZUserAction.ON_QUIT_FULLSCREEN -> Log.i("USER_EVENT", "ON_QUIT_FULLSCREEN" + " title is : " + (if (objects.size == 0) "" else objects[0]) + " url is : " + url + " screen is : " + screen)
                JZUserAction.ON_ENTER_TINYSCREEN -> Log.i("USER_EVENT", "ON_ENTER_TINYSCREEN" + " title is : " + (if (objects.size == 0) "" else objects[0]) + " url is : " + url + " screen is : " + screen)
                JZUserAction.ON_QUIT_TINYSCREEN -> Log.i("USER_EVENT", "ON_QUIT_TINYSCREEN" + " title is : " + (if (objects.size == 0) "" else objects[0]) + " url is : " + url + " screen is : " + screen)
                JZUserAction.ON_TOUCH_SCREEN_SEEK_VOLUME -> Log.i("USER_EVENT", "ON_TOUCH_SCREEN_SEEK_VOLUME" + " title is : " + (if (objects.size == 0) "" else objects[0]) + " url is : " + url + " screen is : " + screen)
                JZUserAction.ON_TOUCH_SCREEN_SEEK_POSITION -> Log.i("USER_EVENT", "ON_TOUCH_SCREEN_SEEK_POSITION" + " title is : " + (if (objects.size == 0) "" else objects[0]) + " url is : " + url + " screen is : " + screen)

                JZUserActionStandard.ON_CLICK_START_THUMB -> Log.i("USER_EVENT", "ON_CLICK_START_THUMB" + " title is : " + (if (objects.size == 0) "" else objects[0]) + " url is : " + url + " screen is : " + screen)
                JZUserActionStandard.ON_CLICK_BLANK -> Log.i("USER_EVENT", "ON_CLICK_BLANK" + " title is : " + (if (objects.size == 0) "" else objects[0]) + " url is : " + url + " screen is : " + screen)
                else -> Log.i("USER_EVENT", "unknow")
            }
        }
    }
}
